package com.fc.v2.util;

import cn.hutool.json.JSONUtil;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.iot.model.v20180120.InvokeThingServiceRequest;
import com.aliyuncs.iot.model.v20180120.InvokeThingServiceResponse;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;

import java.util.HashMap;
import java.util.Map;

/**
 * 阿里云iot
 */
public class IotUtils {

    static String  accessKey = "LTAI4GFCP2zq9oZmJnZnssNf";
    static String accessSecret = "KrP5jZtha8UDTE4xuFgy7DfhdyjTMA";
    static String productKey = "a1udamZbbSp";

//    String deviceName = "kVuFHnqxb0ZbtnW0Z4IK";
    public static InvokeThingServiceResponse publish(String deviceName, Map<String,String> map,String identifier){
        IClientProfile profile = DefaultProfile.getProfile("cn-shanghai", accessKey, accessSecret);
        DefaultAcsClient client = new DefaultAcsClient(profile); //初始化SDK客户端。

//        map.put("keys", "春天");
//        map.put("poem", "五十年来春色好|一枝芳草意如腊|今朝又是人间事|千里风光万壑合");

        InvokeThingServiceRequest ink = new InvokeThingServiceRequest();
        ink.setArgs(JSONUtil.toJsonStr(map));
//        ink.setIdentifier("myWriteTask");
        ink.setIdentifier(identifier);
        ink.setProductKey(productKey);
        ink.setDeviceName(deviceName);

        try {
            InvokeThingServiceResponse acsResponse = client.getAcsResponse(ink);
            System.out.println(acsResponse.getSuccess());
            System.out.println(acsResponse.getCode());
            System.out.println(acsResponse.getErrorMessage());
            return acsResponse;
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            System.out.println("ErrCode:" + e.getErrCode());
            System.out.println("ErrMsg:" + e.getErrMsg());
            e.printStackTrace();
        }
        return null;
    }
}
